home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / network / ka9q / nhclb120.zoo / sys5unix.c < prev    next >
C/C++ Source or Header  |  1992-07-27  |  10KB  |  581 lines

  1. /*
  2.     FILE: unix.c
  3.  
  4.     Routines: This file contains the following routines:
  5.         fileinit()
  6.         eihalt()
  7.         kbread()
  8.         clksec()
  9.         tmpfile()
  10.         restore()
  11.         stxrdy()
  12.         disable()
  13.         memstat()
  14.         filedir()
  15.         sysreset()
  16.  
  17.     Written by Mikel Matthews, N9DVG
  18.     SYS5 stuff added by Jere Sandidge, K4FUM
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <signal.h>
  23. #include <termio.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <sys/times.h>
  27. #include <time.h>
  28. #include <string.h>
  29. #include <ctype.h>
  30.  
  31. /* SCO only ? */
  32. #include <malloc.h>
  33.  
  34. #undef    toupper
  35. #undef    tolower
  36.  
  37. #include "global.h"
  38. #include "config.h"
  39. #include "cmdparse.h"
  40. #include "iface.h"
  41. #include "unix.h"
  42. #include <dirent.h>
  43.  
  44. #define    MAXCMD    1024
  45.  
  46. int asy_attach();
  47. #ifdef    SUNOS4
  48. char *sprintf();
  49. #endif
  50.  
  51. extern struct cmds attab[];
  52. extern struct termio savecon;
  53.  
  54. unsigned long selmask = 0;
  55.  
  56. unsigned nasy;
  57.  
  58. fileinit(argv0)
  59. char *argv0;
  60. {
  61.     int el;
  62.     char *ep, *cp, *getenv(), *getcwd();
  63. #ifndef SCO32
  64.     char *malloc();
  65. #endif
  66.     char tmp[MAXCMD];
  67.     extern char *startup, *config, *userfile, *Dfile, *hosts, *mailspool;
  68.     extern char *mailqdir, *mailqueue, *routeqdir, *alias, *netexe;
  69. #ifdef    _FINGER
  70.     extern char *fingerpath;
  71. #endif
  72. #ifdef    XOBBS
  73.     extern char *bbsexe;
  74. #endif
  75.  
  76.     /* Get the name of the currently executing program */
  77.     if ((cp = malloc((unsigned)(strlen(argv0) + 1))) == NULL)
  78.         perror("malloc");
  79.     else {
  80.         sprintf(cp, "%s", argv0);
  81.         netexe = cp;
  82.     }
  83.  
  84. #ifdef    XOBBS
  85.     /* Get the path to the W2XO BBS executable. */
  86.     if ((ep = getenv("XOBBS")) == NULLCHAR) {
  87.         bbsexe = "xobbs";
  88.     } else {
  89.         if ((cp = malloc((unsigned)(strlen(ep) + 2))) == NULL)
  90.             perror("malloc");
  91.         else {
  92.             sprintf(cp, "%s", ep);
  93.             bbsexe = cp;
  94.         }
  95.     }
  96. #endif
  97.     /* Try to get home directory name */
  98.     if ((ep = getenv("NETHOME")) == NULLCHAR) {
  99.         if ((ep = getenv("HOME")) == NULLCHAR) {
  100.             ep = ".";
  101.         }
  102.     }
  103.     el = strlen(ep);
  104.     /* Replace each of the file name strings with the complete path */
  105.     if (*startup != '/') {
  106.         if ((cp = malloc((unsigned)(el + strlen(startup) + 2))) == NULL)
  107.             perror("malloc");
  108.         else {
  109.             sprintf(cp, "%s/%s", ep, startup);
  110.             startup = cp;
  111.         }
  112.     }
  113.  
  114.     if (*config != '/') {
  115.         if ((cp = malloc((unsigned)(el + strlen(config) + 2))) == NULL)
  116.             perror("malloc");
  117.         else {
  118.             sprintf(cp, "%s/%s", ep, config);
  119.             config = cp;
  120.         }
  121.     }
  122.  
  123.     if (*userfile != '/') {
  124.         if ((cp = malloc((unsigned)(el + strlen(userfile) + 2))) == NULL)
  125.             perror("malloc");
  126.         else {
  127.             sprintf(cp, "%s/%s", ep, userfile);
  128.             userfile = cp;
  129.         }
  130.     }
  131.  
  132.     if (*Dfile != '/') {
  133.         if ((cp = malloc((unsigned)(el + strlen(Dfile) + 2))) == NULL)
  134.             perror("malloc");
  135.         else {
  136.             sprintf(cp, "%s/%s", ep, Dfile);
  137.             Dfile = cp;
  138.         }
  139.     }
  140.  
  141.     if (*hosts != '/') {
  142.         if ((cp = malloc((unsigned)(el + strlen(hosts) + 2))) == NULL)
  143.             perror("malloc");
  144.         else {
  145.             sprintf(cp, "%s/%s", ep, hosts);
  146.             hosts = cp;
  147.         }
  148.     }
  149.  
  150.     if (*alias != '/') {
  151.         if ((cp = malloc((unsigned)(el + strlen(alias) + 2))) == NULL)
  152.             perror("malloc");
  153.         else {
  154.             sprintf(cp, "%s/%s", ep, alias);
  155.             alias = cp;
  156.         }
  157.     }
  158.  
  159. #ifdef        _FINGER
  160.     if (*fingerpath != '/') {
  161.         if ((cp = malloc((unsigned)(el + strlen(fingerpath) + 2))) == NULL)
  162.             perror("malloc");
  163.         else {
  164.             sprintf(cp, "%s/%s", ep, fingerpath);
  165.             fingerpath = cp;
  166.         }
  167.     }
  168. #endif
  169.  
  170.     /* Try to get home directory name */
  171.     if ((ep = getenv("NETSPOOL")) == NULLCHAR)
  172.         ep = "/usr/spool";
  173.     el = strlen(ep);
  174.  
  175.     if (*mailspool != '/') {
  176.         if ((cp = malloc((unsigned)(el + strlen(mailspool) + 2))) == NULL)
  177.             perror("malloc");
  178.         else {
  179.             sprintf(cp, "%s/%s", ep, mailspool);
  180.             mailspool = cp;
  181.         }
  182.     }
  183.  
  184.     if (*mailqdir != '/') {
  185.         if ((cp = malloc((unsigned)(el + strlen(mailqdir) + 2))) == NULL)
  186.             perror("malloc");
  187.         else {
  188.             sprintf(cp, "%s/%s", ep, mailqdir);
  189.             mailqdir = cp;
  190.         }
  191.     }
  192.  
  193.     if (*mailqueue != '/') {
  194.         if ((cp = malloc((unsigned)(el + strlen(mailqueue) + 2))) == NULL)
  195.             perror("malloc");
  196.         else {
  197.             sprintf(cp, "%s/%s", ep, mailqueue);
  198.             mailqueue = cp;
  199.         }
  200.     }
  201.  
  202.     if (*routeqdir != '/') {
  203.         if ((cp = malloc((unsigned)(el + strlen(routeqdir) + 2))) == NULL)
  204.             perror("malloc");
  205.         else {
  206.             sprintf(cp, "%s/%s", ep, routeqdir);
  207.             routeqdir = cp;
  208.         }
  209.     }
  210.  
  211. }
  212.  
  213. /* action routine for remote reset */
  214. sysreset()
  215. {
  216.     extern char *netexe;
  217.  
  218.     execlp(netexe,netexe,0);
  219.     execlp("net","net",0);
  220.     printf("reset failed: exiting\n");
  221.     exit(1);
  222. }
  223.  
  224. long waittime = 100;
  225.  
  226. dowait(argc,argv)
  227. int argc;
  228. char *argv[];
  229. {
  230.   if (argc < 2)  {
  231.     printf("Wait in main select is %d msec\n", waittime);
  232.     return;
  233.   }
  234.   waittime = atoi(argv[1]);
  235. }
  236.  
  237. eihalt()
  238. {
  239.     void tnix_scan();
  240.     struct timeval {
  241.         long    tv_sec;        /* seconds */
  242.         long    tv_usec;    /* microseconds */
  243.     } tv;
  244.     unsigned long mask;
  245.  
  246.     tnix_scan();
  247.  
  248.     if (waittime) {
  249.       tv.tv_sec = waittime / 1000;
  250.       tv.tv_usec = (waittime % 1000) * 1000;
  251.       mask = selmask | 1;
  252.       select(20,&mask,0,0,&tv);
  253.     }
  254. }
  255.  
  256.  
  257. kbread()
  258. {
  259.     unsigned char c;
  260.  
  261.     if (read(fileno(stdin), &c, 1) <= 0)
  262.         return -1;
  263.  
  264.     return ((int) c);
  265. }
  266.  
  267.  
  268. clksec()
  269. {
  270.     time_t tim, time();
  271.  
  272.     (void) time(&tim);
  273.  
  274.     return ((int) tim);
  275. }
  276.  
  277.  
  278. /*ARGSUSED*/
  279. restore(state)
  280. char state;
  281. {
  282. }
  283.  
  284.  
  285. /*ARGSUSED*/
  286. stxrdy(dev)
  287. int16 dev;
  288. {
  289.     return 1;
  290. }
  291.  
  292.  
  293. disable()
  294. {
  295. }
  296.  
  297.  
  298. memstat()
  299. /* This code may only work with the SCO malloc library */
  300. {
  301. /* from "man S malloc" in the SCO manual */
  302.     
  303.   
  304.   char *malloc, *cp;
  305.   struct mallinfo minfo;
  306.   
  307.   minfo = mallinfo();
  308.   printf("malloc arena=%d %d %d0\n",
  309.      minfo.arena, minfo.ordblks, minfo.uordblks);
  310.   return 0;
  311. }
  312.  
  313.  
  314. /* wildcard filename lookup */
  315. filedir(name, times, ret_str)
  316. char    *name;
  317. int    times;
  318. char    *ret_str;
  319. {
  320.     static char    dname[128], fname[128];
  321.     static DIR *dirp = NULL;
  322.     struct dirent *dp;
  323.     struct stat sbuf;
  324.     char    *cp, temp[128];
  325.  
  326.     /*
  327.      * Make sure that the NULL is there in case we don't find anything
  328.      */
  329.     ret_str[0] = '\0';
  330.     if (times == 0) {
  331.         /* default a null name to *.* */
  332.         if (name == NULL || *name == '\0')
  333.             name = "*.*";
  334.         /* split path into directory and filename */
  335.         if ((cp = strrchr(name, '/')) == NULL) {
  336.             strcpy(dname, ".");
  337.             strcpy(fname, name);
  338.         } else {
  339.             strcpy(dname, name);
  340.             dname[cp - name] = '\0';
  341.             strcpy(fname, cp + 1);
  342.             /* root directory */
  343.             if (dname[0] == '\0')
  344.                 strcpy(dname, "/");
  345.             /* trailing '/' */
  346.             if (fname[0] == '\0')
  347.                 strcpy(fname, "*.*");
  348.         }
  349.         /* close directory left over from another call */
  350.         if (dirp != NULL)
  351.             closedir(dirp);
  352.         /* open directory */
  353.         if ((dirp = opendir(dname)) == NULL) {
  354.             printf("Could not open DIR (%s)\n", dname);
  355.             return;
  356.         }
  357.     } else {
  358.         /* for people who don't check return values */
  359.         if (dirp == NULL)
  360.             return;
  361.     }
  362.  
  363.     /* scan directory */
  364.     while ((dp = readdir(dirp)) != NULL) {
  365.         /* test for name match */
  366.         if (wildmat(dp->d_name, fname)) {
  367.             /* test for regular file */
  368.             sprintf(temp, "%s/%s", dname, dp->d_name);
  369.             if (stat(temp, &sbuf) < 0)
  370.                 continue;
  371.             if ((sbuf.st_mode & S_IFMT) != S_IFREG)
  372.                 continue;
  373.             strcpy(ret_str, dp->d_name);
  374.             break;
  375.         }
  376.     }
  377.  
  378.     /* close directory if we hit the end */
  379.     if (dp == NULL) {
  380.         closedir(dirp);
  381.         dirp = NULL;
  382.     }
  383. }
  384.  
  385.  
  386. /* checks the time then ticks and updates ISS */
  387. void
  388. check_time()
  389. {
  390.     int32 iss();
  391.     long times();
  392.  
  393.     struct tms tb;
  394.     static long clkval;
  395.     long ntime, offset;
  396.  
  397.     /* read elapsed real time (typ. 60 Hz) */
  398.     ntime = times(&tb);
  399.  
  400.     /* resynchronize if the error is large (10 seconds or more) */
  401.     offset = ntime - clkval;
  402.     if (offset > (10000/MSPTICK) || offset < 0)
  403.         clkval = ntime;
  404.  
  405.     /* Handle possibility of several missed ticks */
  406.     while (ntime != clkval) {
  407.         ++clkval;
  408.         icmpclk();
  409.         tick();
  410.         (void) iss();
  411.     }
  412. }
  413.  
  414.  
  415. getds()
  416. {
  417.     return 0;
  418. }
  419.  
  420.  
  421. audit()
  422. {
  423. }
  424.  
  425.  
  426. doshell(argc, argv)
  427. char    **argv;
  428. {
  429.     int    i, stat, pid, pid1;
  430. #ifndef SYS5
  431.         int (*savi)();
  432. #else
  433. /* shut GNUC up */
  434.     void (*savi)();
  435. #endif
  436.     char    *cp, str[MAXCMD], *getenv();
  437.     struct termio tt_config;
  438.  
  439.     str[0] = '\0';
  440.     for (i = 1; i < argc; i++) {
  441.         strcat(str, argv[i]);
  442.         strcat(str, " ");
  443.     }
  444.  
  445.     ioctl(0, TCGETA, &tt_config);
  446.     ioctl(0, TCSETAW, &savecon);
  447.  
  448.     if ((cp = getenv("SHELL")) == NULL || *cp != '\0')
  449.         cp = "/bin/sh";
  450.  
  451.     if ((pid = fork()) == 0) {
  452.         if (argc > 1)
  453.             (void)execl("/bin/sh", "sh", "-c", str, 0);
  454.         else
  455.             (void)execl(cp, cp, (char *)0, (char *)0, 0);
  456.         perror("execl");
  457.         exit(1);
  458.     } else if (pid == -1) {
  459.         perror("fork");
  460.         stat = -1;
  461.     } else {
  462.         savi = signal(SIGINT, SIG_IGN);
  463.         while ((pid